//
//  WJDataManager.m
//  WJBSApp
//
//  Created by dllo on 16/7/25.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJDataManager.h"
#import "FMDB.h"
#import "WJMeModel.h"

@interface WJDataManager ()

@property (nonatomic, retain) FMDatabase *fmdb;

@end

@implementation WJDataManager

+ (instancetype)shareFMDB
{
    static WJDataManager *fmdb = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        fmdb = [[WJDataManager alloc]init];
    });
    return fmdb;
}

// 这是创建文件
- (void)openDB
{
    NSString *filePatch = [WJDOCUMENT stringByAppendingPathComponent:@"follow.sqlite"];
    self.fmdb = [[FMDatabase alloc] initWithPath:filePatch];
    BOOL result = [self.fmdb open];
    if (result) {
        NSLog(@"=->OPEN DB Successd");
    } else {
        NSLog(@"=->OPEN DB Failure");
    }
}


// 这是创建文件里面的表格
- (void)createFollowTable
{
    NSString *sqlStr  = @"create table if not exists follow (userID text primary key ,username text,tiezi_count integer,follow_count text,profile_image text) ";
    
    [self executeSqlWithStr:sqlStr showLog:@"CREATE FOllOW TABLE"];
}

//增 (由于主键只有一个增加相同的内容必然失败) 所以这个可以用来判断这里是否拥有
- (void)insertWithID:(NSString *)userID username:(NSString *)username tiezi_count:(NSInteger)tiezi_count follow_count:(NSString *)follow_count profile_image:(NSString *)profile_image
{
    NSString *sqlStr =[NSString stringWithFormat:@"insert into follow (userID,username,tiezi_count,follow_count,profile_image) values ('%@','%@','%ld','%@','%@')",userID,username,tiezi_count,follow_count,profile_image];
    
    [self executeSqlWithStr:sqlStr showLog:@"INSERT "];
}

//删
- (void)deleteWith:(NSString *)userID
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from follow where userID = '%@' ",userID];
    
    [self executeSqlWithStr:sqlStr showLog:@"DELETE FOLLOW"];
}


//查
- (NSMutableArray *)selectAll
{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"select *from follow"];
    //执行查询语句
    FMResultSet *set = [self.fmdb executeQuery:sql];
    //FMResultSet 是用来遍历表中符合要求的每一行
    while ([set next]) {
        WJLastModel *model = [[WJLastModel alloc]init];
        
        model.id = [set objectForColumnName:@"userID"];
        model.username = [set objectForColumnName:@"username"];
        model.tiezi_count = [set intForColumn:@"tiezi_count"];
        model.follow_count = [set objectForColumnName:@"follow_count"];
        model.profile_image = [set objectForColumnName:@"profile_image"];
        
        [array addObject:model];
    }
    
    return array;
}

- (BOOL)isExistUserID:(NSString *)userID
{
    NSMutableArray *array = [self selectAll];
    
    for (WJLastModel *model in array) {
        
        if ([model.id isEqualToString:userID]) {
            return YES;
        }
        
    }
    
    return NO;
}

#pragma mark - 封装方法
- (void)executeSqlWithStr:(NSString *)sql showLog:(NSString *)message{
    //执行语句
    BOOL result = [self.fmdb executeUpdate:sql];
    NSString *string = @"";
    if (result) {
        string = [NSString stringWithFormat:@"%@ Succeed", message];
    } else {
        string = [NSString stringWithFormat:@"%@ Failure", message];
    }
    NSLog(@"=->%@", string);
}









#pragma mark - 第一个分区的东西  五个参数    数 串 数 数 串

// 创建订阅的表格
- (void)createFirTable
{
    NSString *sqlStr  = @"create table if not exists fir (theme_id integer primary key,image_list text,sub_number integer, post_num integer, theme_name text) ";
    
    [self executeSqlWithStr:sqlStr showLog:@"CREATE fir TABLE"];
}

// 增 订阅
- (void)insertToFirTheme_id:(NSInteger)theme_id Image_list:(NSString *)image_list Sub_number:(NSInteger)sub_number Post_num:(NSInteger)post_num Theme_name:(NSString *)theme_name
{
    NSString *sqlStr =[NSString stringWithFormat:@"insert into fir (theme_id,image_list,sub_number,post_num,theme_name) values ('%ld','%@','%ld','%ld','%@')",theme_id,image_list,sub_number,post_num,theme_name];
    
    [self executeSqlWithStr:sqlStr showLog:@"INSERT "];
}

// 删 订阅
- (void)deleteFromFirWithTheme_id:(NSInteger)theme_id
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from fir where theme_id = '%ld' ",theme_id];
    
    [self executeSqlWithStr:sqlStr showLog:@"DELETE fir"];
}

// 查 订阅
- (NSMutableArray *)selectAllFir
{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"select *from fir"];
    //执行查询语句
    FMResultSet *set = [self.fmdb executeQuery:sql];
    //FMResultSet 是用来遍历表中符合要求的每一行
    while ([set next]) {
        
        int set_theme_id = [set intForColumn:@"theme_id"];
        NSString *set_image_list = [set objectForColumnName:@"image_list"];
        int set_sub_number = [set intForColumn:@"sub_number"];
        int set_post_num = [set intForColumn:@"post_num"];
        NSString *set_theme_name = [set objectForColumnName:@"theme_name"];
        
        WJMeModel *model = [[WJMeModel alloc]init];
        model.theme_id = set_theme_id;
        model.image_list = set_image_list;
        model.sub_number = set_sub_number;
        model.post_num = set_post_num;
        model.theme_name = set_theme_name;
        
        [array addObject:model];
        [model release];
        
    }
    
    return array;
}


// 判断里面是否拥有这个订阅
- (BOOL)isExistTheme_id:(NSInteger)Theme_id
{
    NSMutableArray *array = [self selectAllFir];
    
    for (WJMeModel *model in array) {
        
        if (model.theme_id == Theme_id) {
            return YES;
        }
    }
    return NO;
}
//@property (nonatomic, assign) NSInteger theme_id;
//// 小图像
//@property (nonatomic, copy) NSString *image_list;
//// 订阅数
//@property (nonatomic, assign) NSInteger sub_number;
//// 总帖数
//@property (nonatomic, assign) NSInteger post_num;
//// 名字
//@property (nonatomic, copy) NSString *theme_name;
//












#pragma mark - 第二个分区的东西

// 创建订阅的表格
- (void)createSecTable
{
    NSString *sqlStr  = @"create table if not exists sec (theme_id integer primary key,image_list text,sub_number integer, post_num integer, theme_name text) ";
    
    [self executeSqlWithStr:sqlStr showLog:@"CREATE sec TABLE"];
}

// 增 订阅
- (void)insertToSecTheme_id:(NSInteger)theme_id Image_list:(NSString *)image_list Sub_number:(NSInteger)sub_number Post_num:(NSInteger)post_num Theme_name:(NSString *)theme_name
{
    NSString *sqlStr =[NSString stringWithFormat:@"insert into sec (theme_id,image_list,sub_number,post_num,theme_name) values ('%ld','%@','%ld','%ld','%@')",theme_id,image_list,sub_number,post_num,theme_name];
    
    [self executeSqlWithStr:sqlStr showLog:@"INSERT "];
}

// 删 订阅
- (void)deleteFromSecTheme_id:(NSInteger)theme_id
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from sec where theme_id = '%ld' ",theme_id];
    
    [self executeSqlWithStr:sqlStr showLog:@"DELETE sec"];
}

// 查 订阅
- (NSMutableArray *)selectAllSec
{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"select *from sec"];
    //执行查询语句
    FMResultSet *set = [self.fmdb executeQuery:sql];
    //FMResultSet 是用来遍历表中符合要求的每一行
    while ([set next]) {
        
        int set_theme_id = [set intForColumn:@"theme_id"];
        NSString *set_image_list = [set objectForColumnName:@"image_list"];
        int set_sub_number = [set intForColumn:@"sub_number"];
        int set_post_num = [set intForColumn:@"post_num"];
        NSString *set_theme_name = [set objectForColumnName:@"theme_name"];
        
        WJMeModel *model = [[WJMeModel alloc]init];
        model.theme_id = set_theme_id;
        model.image_list = set_image_list;
        model.sub_number = set_sub_number;
        model.post_num = set_post_num;
        model.theme_name = set_theme_name;
        
        [array addObject:model];
        [model release];
        
    }
    
    return array;
}
// 判断里面是否拥有这个订阅
- (BOOL)isExistSecTheme_id:(NSInteger)Theme_id
{
    NSMutableArray *array = [self selectAllSec];
    
    for (WJMeModel *model in array) {
        
        if (model.theme_id == Theme_id) {
            return YES;
        }
    }
    return NO;
}






 /********************* 发 表 的 ***********************/
- (void)createPublishTable
{
    NSString *sqlStr  = @"create table if not exists PubTable (number integer primary key  autoincrement, type text,pubText text,nowTime text,links text,picData blob,videoPath text,audioPath text,totalTime text) ";
    
    [self executeSqlWithStr:sqlStr showLog:@"CREATE PubTable TABLE"];
}

// 增  真尼玛坑爹啊自己转化
- (void)insertModel:(WJPublishModel *)model
{
    NSString *insertSql = @"insert into PubTable (type,pubText,nowTime,links,picData,videoPath,audioPath,totalTime) values (?,?,?,?,?,?,?,?)";
    //executeUpdate:后面跟的参数类型必须是对象类型
    //FMDataBase对象会将传过来的参数，转化成与数据库字段相匹配的类型，再进行后续处理
    [self.fmdb executeUpdate:insertSql,model.type,model.text,model.nowTime,model.links,model.picData,model.videoPath,model.audioPath,model.totalTime];
}

// 删 满足条件的都会删除  
- (void)deleteFromText:(NSString *)nowTime
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from PubTable where nowTime = '%@' ",nowTime];
    
    [self executeSqlWithStr:sqlStr showLog:@"DELETE PubTable"];
    
}

// 查
- (NSMutableArray *)selectAllPublish
{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"select *from PubTable"];
    //执行查询语句
    FMResultSet *set = [self.fmdb executeQuery:sql];
    //FMResultSet 是用来遍历表中符合要求的每一行
    while ([set next]) {
        
        WJPublishModel *model = [[WJPublishModel alloc]init];
        model.type = [set objectForColumnName:@"type"];
        model.text = [set objectForColumnName:@"pubText"];
        model.nowTime = [set objectForColumnName:@"nowTime"];
        model.links = [set objectForColumnName:@"links"];
        model.picData = [set dataForColumn:@"picData"];
        model.videoPath = [set objectForColumnName:@"videoPath"];
        model.audioPath = [set objectForColumnName:@"audioPath"];
        model.totalTime = [set objectForColumnName:@"totalTime"];
        
        [array addObject:model];
    }
    return array;
}

 /********************* 发 表 的 ***********************/

- (void)createPicTable
{
    NSString *sqlStr  = @"create table if not exists PicTable (picName text primary key) ";

    [self executeSqlWithStr:sqlStr showLog:@"CREATE PicTable TABLE"];
}

// 增
- (void)insertPicName:(NSString *)picName
{
    NSString *sqlStr =[NSString stringWithFormat:@"insert into PicTable (picName) values ('%@')",picName];
    
    [self executeSqlWithStr:sqlStr showLog:@"INSERT"];
    
}

// 查
- (NSMutableArray *)selectAllPic
{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"select *from PicTable"];
    //执行查询语句
    FMResultSet *set = [self.fmdb executeQuery:sql];
    //FMResultSet 是用来遍历表中符合要求的每一行
    while ([set next]) {
        NSString *set_picName = [set objectForColumnName:@"picName"];
        [array addObject:set_picName];
    }
    return array;
}


- (BOOL)isExistPicName:(NSString *)picName
{
    NSMutableArray *array = [self selectAllPic];
    
    for (NSString *pic in array) {
        
        if ([pic isEqualToString:picName]) {
            return YES;
        }
    }
    return NO;
}

- (void)deleteAllPic
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from PicTable"];
    
    [self executeSqlWithStr:sqlStr showLog:@"DELETE PicTable"];

    
}




















@end
//// *********************段子***********************
//- (void)createPublishTable
//{
//    NSString *sqlStr  = @"create table if not exists PubTextTable (number integer primary key  autoincrement, type text ,pubText text) ";
//    
//    [self executeSqlWithStr:sqlStr showLog:@"CREATE PubTextTable TABLE"];
//}
//
//// 增
//- (void)insertType:(NSString *)type text:(NSString *)text
//{
//    NSString *sqlStr =[NSString stringWithFormat:@"insert into PubTextTable (type,pubText) values ('%@','%@')",type,text];
//    
//    [self executeSqlWithStr:sqlStr showLog:@"INSERT "];
//}
//
//// 删
//- (void)deleteFromText:(NSString *)text
//{
//    NSString *sqlStr = [NSString stringWithFormat:@"delete from PubTextTable where pubText = '%@' ",text];
//    
//    [self executeSqlWithStr:sqlStr showLog:@"DELETE PubTextTable"];
//    
//}
//
//// 查
//- (NSMutableArray *)selectAllText
//{
//    NSMutableArray *array = [NSMutableArray array];
//    
//    NSString *sql = [NSString stringWithFormat:@"select *from PubTextTable"];
//    //执行查询语句
//    FMResultSet *set = [self.fmdb executeQuery:sql];
//    //FMResultSet 是用来遍历表中符合要求的每一行
//    while ([set next]) {
//        NSString *set_type = [set objectForColumnName:@"type"];
//        NSString *set_name = [set objectForColumnName:@"pubText"];
//        
//        WJPublishModel *model = [[WJPublishModel alloc]init];
//        model.type = set_type;
//        model.text = set_name;
//        
//        [array addObject:model];
//    }
//    return array;
//}
//
//
//
//// *********************链接***********************
//- (void)createPublishLinksTable
//{
//    NSString *sqlStr  = @"create table if not exists PubLinksTable (number integer primary key  autoincrement, type text ,links text,pubText text) ";
//    
//    [self executeSqlWithStr:sqlStr showLog:@"CREATE PubLinksTable TABLE"];
//}
//
//// 增
//- (void)insertType:(NSString *)type links:(NSString *)links text:(NSString *)text
//{
//    NSString *sqlStr =[NSString stringWithFormat:@"insert into PubLinksTable (type,links,pubText) values ('%@','%@','%@')",type,links,text];
//    
//    [self executeSqlWithStr:sqlStr showLog:@"INSERT"];
//    
//    
//}
//
//// 删
//- (void)deleteFromLinks:(NSString *)Links
//{
//    NSString *sqlStr = [NSString stringWithFormat:@"delete from PubLinksTable where links = '%@' ",Links];
//    
//    [self executeSqlWithStr:sqlStr showLog:@"DELETE PubLinksTable"];
//}
//
//// 查
//- (NSMutableArray *)selectAllLinks
//{
//    NSMutableArray *array = [NSMutableArray array];
//    
//    NSString *sql = [NSString stringWithFormat:@"select *from PubLinksTable"];
//    //执行查询语句
//    FMResultSet *set = [self.fmdb executeQuery:sql];
//    //FMResultSet 是用来遍历表中符合要求的每一行
//    while ([set next]) {
//        NSString *set_type = [set objectForColumnName:@"type"];
//        NSString *set_links = [set objectForColumnName:@"links"];
//        NSString *set_name = [set objectForColumnName:@"pubText"];
//        
//        WJPublishModel *model = [[WJPublishModel alloc]init];
//        model.type = set_type;
//        model.links = set_links;
//        model.text = set_name;
//        
//        [array addObject:model];
//    }
//    return array;
//}
//
//
//// *********************图片***********************
